package smr.test;

import smr.client.model.vo.AudioFile;
import smr.client.model.vo.VideoFile;

/**
 * 请求进程
 * @author tanggf(2012-3-5)
 *
 */
public class Progress {
	/**  请求开始时间 */
	private long start;
	/** 解析url时间  */
	private long middle;	
	/** 下载结束时间 */
	private long end;
	/** 下载文件总大小 */
	private long total;	
	/**  已下载文件内容大小 */
	private long downloaded;
	/**  标记是否已完成下载*/
	private boolean done;		
	/** 下载文件保存路径 */
	private String saveFile;
	/** 视频文件信息 */
	private VideoFile videoFile;
	/** 音频文件信息 */
	private AudioFile audioFile;	
	private TestConfig config;
	/** 视频url */
	private String pageUrl;
	/**  标记进程失败*/
	private boolean fail;
	/** 文件标题信息 */
	private String title;
	/** 是否已开始下载文件 */
	private boolean startDownload;
	/** 错误信息 */
	private String msg;
	
	public Progress(TestConfig config) {
		this.config = config;
	}
	
	public String getPageUrl() {
		return pageUrl;
	}
	public void setPageUrl(String pageUrl) {
		this.pageUrl = pageUrl;
	}
	public boolean isFail() {
		return fail;
	}
	public void setFail(boolean fail) {
		this.fail = fail;
	}
	public VideoFile getVideoFile() {
		return videoFile;
	}
	public void setVideoFile(VideoFile videoFile) {
		this.videoFile = videoFile;
	}
	public void start(){
		this.start = System.currentTimeMillis();
	}
	
	public void end(){
		this.end = System.currentTimeMillis();
	}
	
	public String summary(){		
		if(isDone()){
			String type = getConfig().isVideoFile() ? "video":"audio";
			//解析url时间
			long parseTime = getParseTime();		
			String s = "the server took [" + parseTime + "seconds] to parse the " + type + " url[" + getPageUrl() + "]";
			
			if(getConfig().isDownloadFile()){
				//下载文件时间
				long downloadTime = end - middle;
				long rate = getTotal() * 1000 / downloadTime;				
				s += "\n the client took [" + downloadTime + "seconds] to download the " + type + "{" + getInfo() + "}, the rate is [" + rate + "byte/s]";
			}			
			return s;
		}else{
			return "not ready to summary the progress";
		}
	}	
	
	/**
	 * 进度信息
	 * @return
	 */
	public String getProcessInfo(){
		if(isStartDownload()){
			
		}else{
			
		}
		return null;		
	}
	
	/**
	 * 解析url时间(秒)
	 * @return
	 */
	public long getParseTime(){
		if(middle > 0){
			return (middle - start)/1000;
		}
		return 0;
	}
	
	/**
	 * 下载文件时间(秒)
	 * @return
	 */
	public long getDownloadTime(){
		if(end > 0){
			return (end - middle) / 1000;
		}
		return 0;
	}
	
	public String getInfo(){
		if(config.isVideoFile()){
			return getVideoInfo();
		}
		return getAudioInfo();
	}
	
	private String getAudioInfo() {
		return "<<" + audioFile.getFilename() + ">>";
	}

	public String getVideoInfo() {
		return  "<<" + getTitle() + ">>，VideoType: " + videoFile.getVideoType() + ", Quality: " + videoFile.getQuality();		
	}	
	public double getPercent(){				
		return Math.floor((downloaded / (total * 1.0d)) * 100);
	}
	
	public void setStart(long start) {		
		this.start = start;
	}	
	public void setMiddle(long middle) {
		if(this.config.isVideoFile()){
			
		}
		this.middle = middle;
	}
	public void setEnd(long end) {
		this.end = end;
	}
	
	
	public void addDownloaded(long got){
		this.downloaded += got;		
	}
	public long getTotal() {
		return total;
	}
	public void setTotal(long total) {
		this.total = total;
	}
	public long getDownloaded() {
		return downloaded;
	}
	public void setDownloaded(long downloaded) {
		this.downloaded = downloaded;
	}
	public boolean isDone() {
		return done;
	}
	public void setDone(boolean done) {
		this.done = done;
	}		
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}	
	public String getSaveFile() {
		return saveFile;
	}
	public void setSaveFile(String saveFile) {
		this.saveFile = saveFile;
	}
	public long getStart() {
		return start;
	}	
	public long getEnd() {
		return end;
	}	
	public long getMiddle() {
		return middle;
	}	
	public AudioFile getAudioFile() {
		return audioFile;
	}
	public void setAudioFile(AudioFile audioFile) {
		this.audioFile = audioFile;
	}
	public TestConfig getConfig() {
		return config;
	}
	public void setConfig(TestConfig config) {
		this.config = config;
	}
	public boolean isStartDownload() {
		return startDownload;
	}
	public void setStartDownload(boolean startDownload) {
		this.startDownload = startDownload;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}	
}
